<template>
	<view class="container">
		<navi-shell :back="true" v-bind:title="nick[0]+'老师的'+courses.length+'门课程'">
			<uni-search-bar placeholder="请输入搜索关键字" class="uni-searchbar" :redius="100" cancelButton="none"
				@confirm="search" @clear="clearKeywords"></uni-search-bar>
		</navi-shell>
		<view v-if="isLogin" v-for="(course,index) in courses" :key="course.id" class="course-display">
			<image :src="course.cover" mode="aspectFill" class="img-cover" @click.stop="clickShowUser(course)"></image>
			<text class="course-name">{{course.name}}</text>
			<text class="course-teacher">{{nick}}</text>
			<text class="course-desc">{{course.desc}}</text>
			<text class="course-chapter">共{{course.countChapter}}章</text>
			<text class="iconfont icon-zidingyizhangjie ic-chapter"
				@click="chaptermanage(course.id,course.name)"></text>
			<text class="course-student">共{{course.countStudent}}人</text>
			<text v-if="course.open == false" class="iconfont icon-gouxuan ic-open"
				@click="courseOpenClose(course)"></text>
			<text v-if="course.open == true" class="iconfont icon-gouxuan1 ic-open"
				@click="courseOpenClose(course)"></text>
		</view>
		<dialog-shell ref="shell" v-bind:title="title">
			<text class="course-descnews">{{courseDesc}}</text>
		</dialog-shell>
	</view>
</template>

<script>
	const app = getApp();
	export default {
		data() {
			return {
				isLogin: false,
				courses: [],
				nick: '',
				courseDesc: '',
				title: '',
				cSearch: false,
				searching: false,
			}
		},
		onLoad() {
			this.courseAll()
		},
		mounted() {
			this.isLogin = app.globalData.token.length > 0
			this.nick = app.globalData.nickName
			if (!this.isLogin) {
				delta = 2
				this.$refs.login.show()
			}
		},
		onPullDownRefresh() {
			this.courses = [],
				console.log("执行了下拉刷新")
			console.log(this.cSearch)
			this.courseAll()
			uni.stopPullDownRefresh()
		},
		methods: {
			courseOpen(course) {
				course.open = !course.open
				let url = this.$params.host + this.$params.course_root + this.$params.course_open_close
				let data = {
					token: app.globalData.token,
					courseId: course.id,
					open: course.open
				}
				console.log(data)
				this.$request.post(url, data, res => {
					console.log(res)
				})
			},
			courseOpenClose(course) {
				if (course.open) {
					uni.showModal({
						content: '关闭课程？',
						showCancel: true,
						cancelText: '否',
						confirmText: '是',
						success: res => {
							if (res.confirm) {
								this.courseOpen(course)
							}
						},
						fail: () => {},
						complete: () => {}
					});
				} else {
					uni.showModal({
						content: '开放课程？',
						showCancel: true,
						cancelText: '否',
						confirmText: '是',
						success: res => {
							if (res.confirm) {
								this.courseOpen(course)
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}
			},
			courseAll() {
				let url = this.$params.host + this.$params.course_root + this.$params.course_teacher
				let data = {
					token: app.globalData.token,
					id: app.globalData.uid
				}
				this.$request.getParams(url, data, res => {
					console.log(res)
					res.data.forEach(b => {
						if (!b.cover.startsWith('http')) {
							b.cover = this.$params.host + b.cover
						}
					})
					this.courses = res.data
				});
			},
			clickShowUser(course) {
				this.courseDesc = course.desc
				this.title = course.name
				this.$refs.shell.showDialog()
			},
			searchKey() {
				this.cSearch = !this.cSearch
			},
			clearKeywords() {
				this.searching = false
				this.courses = []
				this.courseAll()
			},
			search(e) {
				let kw = e.value
				if (kw.trim().length == 0) {
					return
				}
				this.searching = true
				this.courses = []
				let data = {
					token: app.globalData.token
				}
				let url = this.$params.host + this.$params.course_root + this.$params.course_search + kw
				this.$request.getParams(url, data, res => {
					res.data.forEach(b => {
						if (!b.cover.startsWith('http')) {
							b.cover = this.$params.host + b.cover
						}
					})
					this.courses = res.data
					console.log(res)
				})
			},
			chaptermanage(id, name) {
				uni.navigateTo({
					url: '../chaptermanage/chaptermanage?id=' + id + '&name=' + name,
					success: res => {},
					fail: () => {},
					complete: () => {}
				})
			}
		}
	}
</script>

<style>
	@import url("coursemanage.css")
</style>
